var username=localStorage.getItem("username");
var token=localStorage.getItem("token");
var userid=localStorage.getItem("userid");
$(function(){
	var paramMap = params();
	var id = paramMap['id'];
	
	$("#checkSkuProduct").click(function(){
		var skuId = id;
		$.ajax({
			data:{
				id:skuId,
				approveStatus:3,
				token : token
			},
			url:server_url+"rest/saleManager/checkApproveStatus.do",
			success:function(){
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:"审核成功"
					},
					autoClose : 2000
				});
				var url="skuProductInfo.do?id="+id;
				tl_config.loadToWorkArea(url);
			},
			error:function(data){
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:data.msg
					},
					autoClose : 2000
				});
			}
		});
	});
	$("#refuseSkuProduct").click(function(){
		var skuId = id;
		$.ajax({
			data:{
				id:skuId,
				approveStatus:2,
				token : token
			},
			url:server_url+"rest/saleManager/checkApproveStatus.do",
			success:function(){
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:"审核拒绝成功"
					},
					autoClose : 2000
				});
				var url="skuProductInfo.do?id="+id;
				tl_config.loadToWorkArea(url);
			},
			error:function(data){
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:data.msg
					},
					autoClose : 2000
				});
			}
		});
	});
	
	$("#backList").click(function(){
		var url=server_url+'skuProductList.do';
		tl_config.last_url=url;
		tl_config.loadToWorkArea(url);
	});
	$.ajax({
		type : 'GET',
		contentType : 'application/json',
		async : false,
		data : {
			id : id,
			token:token
		},
		url : server_url+'rest/saleManager/getSkuProductInfoById.do?ran='+Math.random(),
		dataType : 'json',
		success : function(data) {
			if (data.retCode == "0000") {
				pasteData(data.data);
			} else{
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:data.retMsg
					},
					autoClose : 2000
				});
			}
		}
	});
	
});

function isEmpty(data){
	if(data==null||data==undefined||data.length==0){
		return true;
	}else{
		return false;
	}
}
function pasteData(data){
	console.log(data);
	var saleStatus = data.saleStatus;
	$("#productName").text(data.productName);
	$("#supplyName").text(data.supplyName);
	$("#specId").text(data.specName);
	$("#catchId").text(data.catchName);
	$("#weightId").text(data.weightName);
	$("#price").text(data.price);
	
	$("#addTime").text(data.addTime);
	$("#overPlusNum").text(data.overPlusNum);
	$("#addUser").text(data.addUser);
	$("#firstIsOffTime").text(data.firstIsOffTime);
	$("#onShelfTime").text(data.onShelfTime);
	$("#offShelfTime").text(data.offShelfTime);
	$("#approveStatus").text(data.approveStatus);
	
	$("#isOffStatus").text(data.isOffStatus);
	$("#saleStatus").text(saleStatus);
	$("#saleType").text(data.saleType);
	$("#auditTime").text(data.auditTime); //TODO
	
	$("#isPayOnline").text(data.isPayOnline);
	$("#PicturePath1").attr("src",data.picturePath1);
	$("#PicturePath2").attr("src",data.picturePath2);
	
	$("#PicturePath3").attr("src",data.picturePath3);
	var areaIds = data.areaId;
	if(areaIds != null){
		var areaId = areaIds.split(",");
		replaceEmptyItem(areaId);
		if(data.saleType == "定向到区域"){
			$("#areaMsg").css("display","block");
			var areaStr = "";
			for(var i = 0;i<areaId.length;i++){
				$.ajax({
					url:server_url+'rest/saleManager/getAreaName.do?areaId='+areaId[i],
					type:'post',
					async:false,
					success:function(dataes){
						areaStr += dataes.data.data[0].buyerAreaName+',';
					}
				})
			}
			$("#address").text(areaStr);
		}
	}
	//if(saleStatus == "链牛指向买家"){
		var buyerIds = data.buyerId;
		if(null != buyerIds){
			$("#pointMsg").css("display","block");
			$("#msgInfo").css("display","block");
			var buyerId = buyerIds.split(",");
			replaceEmptyItem(buyerId);
			$.ajax({
				type : 'GET',
				contentType : 'application/json',
				async : false,
				data : {
					buyerid : buyerId[0],
					token:token
				},
				url : server_url+'/rest/buyerManage/getBuyer.do?ran='+Math.random(),
				dataType : 'json',
				success:function(msg){
					console.log(msg);
					$("<tr><td>"+msg.data.buyer.buyerName+"</td> " +
							"<td>"+msg.data.buyer.buyerTel+"</td>  " +
							"<td>"+msg.data.buyer.provinceName+msg.data.buyer.cityName+msg.data.buyer.countyName+"</td>  " +
							"<td>&nbsp;</td>  " +
							"</tr>").appendTo($("table"));
				}
			})
		};
	//}else if(saleStatus == "链牛指向卖家"){
		
	//}
	
	$("#remark").text(data.remark);
};
function replaceEmptyItem(arr){
    for(var i=0,len=arr.length;i<len;i++){
        if(!arr[i]|| arr[i]==''){
            arr.splice(i,1);
            len--;
             
        }
    }
};
